package tests;
import processing.core.*; 
import processing.xml.*; 

import java.applet.*; 
import java.awt.Dimension; 
import java.awt.Frame; 
import java.awt.event.MouseEvent; 
import java.awt.event.KeyEvent; 
import java.awt.event.FocusEvent; 
import java.awt.Image; 
import java.io.*; 
import java.net.*; 
import java.text.*; 
import java.util.*; 
import java.util.zip.*; 
import java.util.regex.*; 
import processing.video.*;

public class Webcam_diffPic extends PApplet {
	
	final int VIDEO_WIDTH  = 320;
	final int VIDEO_HEIGHT = 240;
	
	float[] buffer1 = new float[VIDEO_WIDTH * VIDEO_HEIGHT];
	float[] buffer2 = new float[buffer1.length];
	float[] buffer3 = new float[buffer1.length];
	
	float threshold = 50;
	Capture cam;
	
	public void setup () {
		size (VIDEO_WIDTH, VIDEO_HEIGHT);
		cam = new Capture (this, VIDEO_WIDTH, VIDEO_HEIGHT, 10);
		frameRate (10);
	}
	
	public void draw () {	
		if (cam.available ()) {
			cam.read ();
			background (40);
			loadPixels ();
		
	
		 for (int i=0; i < cam.pixels.length; i++) {
		   int col = cam.pixels[i];
		   float sum = red (col) + green (col) + blue (col);
		   float deltaPixel = (buffer1[i] + buffer2[i] + buffer3[i]) / 3 - sum;
		   if (deltaPixel > threshold || deltaPixel < -threshold) {
		     pixels[i] = color (255);
		   }
			
		   buffer3[i] = buffer2[i];
		   buffer2[i] = buffer1[i];
		   buffer1[i] = sum;
		 }
		
		 updatePixels ();
		}
		
		filter(INVERT);
	}

}
